from random import random

import pygame
from pygame.sprite import Sprite


class Alien(Sprite):
    def __init__(self, settings, screen):
        super(Alien, self).__init__()
        self.settings = settings
        self.screen = screen
        self.image = pygame.image.load("images/alien.png")
        self.rect = self.image.get_rect()
        # 初始alien在屏幕左上角附近
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # alien精确位置
        self.x = float(self.rect.x)
        # 初始化随机移动速度
        self.speed = random()

    def blitme(self):
        """在屏幕上绘制alien"""
        self.screen.blit(self.image, self.rect)

    def update(self):
        """alien移动"""
        self.x += (self.speed * self.settings.fleet_direction)
        self.rect.x = self.x

    def check_edges(self):
        """判断是否到达左右屏幕边缘"""
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right:
            return True
        elif self.rect.left <= 0:
            return True
        else:
            return False
